home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / freetype.zip / tttypes.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-07  |  15.5 KB  |  481 lines

  1. {****************************************************************************}
  2. {*                                                                          *}
  3. {*  TTTypes                                                                 *}
  4. {*                                                                          *}
  5. {*  This unit contains the data types used by the TrueType engine           *}
  6. {*                                                                          *}
  7. {****************************************************************************}
  8.  
  9. unit TTTypes;
  10.  
  11. interface
  12.  
  13. type
  14.  
  15.   (* IntN types :                                                       *)
  16.   (*                                                                    *)
  17.   (*  These types are used as a way to garantee the size of some        *)
  18.   (*  specific integers.                                                *)
  19.   (*                                                                    *)
  20.  
  21.   Int16 = Integer;            (* 16 bits integer *)
  22.   Int32 = LongInt;            (* 32 ""           *)
  23.   Int64 = record              (* 64 ""           *)
  24.             Hi,
  25.             Lo  : LongInt;
  26.           end;
  27.  
  28.  
  29.   (* BYTE is already defined in Pascal       *)
  30.   (* They are equivalent to C unsigned chars *)
  31.  
  32.   UShort   = Word;          (* unsigned short integer, must be on 16 bits *)
  33.   Short    = Integer;       (* signed short integer,   must be on 16 bits *)
  34.   ShortRec = record
  35.                Low,         (* a structure used for various type casts *)
  36.                High : Byte;
  37.              end;
  38.  
  39.   ULong = LongInt;         (* unsigned long integer, must be on 32 bits *)
  40.                            (* NOTE : There is no 'LongWord' in Pascal,  *)
  41.                            (*        but the unsigned ops are all in    *)
  42.                            (*        the inline assembly routines       *)
  43.  
  44.   Long  = LongInt;         (* signed long integer,   must be on 32 bits *)
  45.  
  46.   Fixed    = Longint;      (* Signed Fixed 16.16 Float *)
  47.   FixedRec = record        (* Structure used for various typecasts *)
  48.                Low,
  49.                High  : Integer;
  50.              end;
  51.  
  52.   FixedPoint = FixedRec;
  53.  
  54. {$IFDEF OS2}
  55.   Int = LongInt;      (* the 'int' type is used for loop counters and  *)
  56. {$ELSE}               (* indexes.. Their size must be the one a given  *)
  57.   Int = Integer;      (* system handles most easily ( 16 bits on Turbo *)
  58. {$ENDIF}              (* and 32 on Virtual Pascals )                   *)
  59.  
  60.   (* FUnits are the distance unit used for the EM square *)
  61.  
  62.   FWord = Short;      (* Distance in FUnits *)
  63.  
  64.   UFWord = UShort;    (* unsigned distance *)
  65.  
  66.   F2Dot14 = Short;    (* signed fixed float 2.14 used for unary vectors *)
  67.                       (* Layout :                                       *)
  68.                       (*                                                *)
  69.                       (*  s : 1  -- sign bit                            *)
  70.                       (*  m : 1  -- mantissa bit                        *)
  71.                       (*  f : 14 -- unsigned fractional part            *)
  72.                       (*                                                *)
  73.                       (*  's:m' is the 2-bit signed integer value to    *)
  74.                       (*  which the positive fractional part should be  *)
  75.                       (*  added.                                        *)
  76.                       (*                                                *)
  77.  
  78.   TUnitVector = record
  79.                  x, y : F2Dot14; (* guess what ? *)
  80.                 end;
  81.  
  82.   F26Dot6 = LongInt;  (* 26.6 Fixed float, used for glyph points'  *)
  83.                       (* pixel coordinates                         *)
  84.   TVector = record
  85.               x, y : F26Dot6;  (* Simple pixel vector *)
  86.             end;
  87.  
  88.   PVecTable = ^TVecTable;
  89.   TVecTable  = Array[0..1000] of TVector;
  90.   (* a vector table type *)
  91.  
  92.   PTouchTable = ^TTouchTable;
  93.   TTouchTable = Array[0..1000] of Byte;
  94.   (* a flag table type *)
  95.  
  96.   TVecRecord = record
  97.                 N     : int;         (* points number                *)
  98.                 Org   : PVecTable;   (* original coordinates from EM *)
  99.                 Cur   : PVecTable;   (* current coordinates          *)
  100.                 Touch : PTouchTable; (* flags table                  *)
  101.                end;
  102.  
  103.   (* This type defining a set of glyph points will be used to represent *)
  104.   (* each zones ( regular and twilight ) during instructions decoding   *)
  105.  
  106.   TContour = record
  107.               First, Last : Int;
  108.              end;
  109.   (* First and Last point index of a contour in a glyph *)
  110.  
  111.   PContourTable = ^TContourTable;
  112.   TContourTable = array[0..15] of TContour;
  113.   (* A contour bounds table *)
  114.  
  115.   TContourRecord = record
  116.                     N : int;           (* Contours numbers *)
  117.                     C : PContourTable; (* Contours table   *)
  118.                    end;
  119.  
  120.   (* This type is used to define the contours of each glyph *)
  121.  
  122.  
  123.   (* Simple access types : pointers and tables *)
  124.  
  125.   PUShort     = ^UShort;
  126.   PShort      = ^Short;
  127.  
  128.   PULong      = ^ULong;
  129.   PLong       = ^Long;
  130.  
  131.   PFixed      = ^Fixed;
  132.   PFixedRec   = ^FixedRec;
  133.  
  134.   TByteArray  = array[0..63999] of Byte;
  135.   PByteArray  = ^TByteArray;
  136.  
  137.   PShortArray = ^TShortArray;
  138.   TShortArray = array[0..1023] of Integer;
  139.  
  140.   TStorage    = array[0..16000] of Long;
  141.   PStorage    = ^TStorage;
  142.  
  143.   TPoint = Record
  144.              GlyphNum : integer; (* this point's glyph number   *)
  145.              V        : TVector; (* current pixel position      *)
  146.              Touch    : Byte;    (* touch flags                 *)
  147.             end;
  148.  
  149.  
  150.   PTTZone = ^TTTZone;
  151.   TTTZone = Array[0..1023] of TPoint;
  152.  
  153.  
  154.  
  155.   TCVTRecord  = record
  156.                  N : int;       (* size in 32 bits elements *)
  157.                  A : PStorage;  (* table address            *)
  158.                 end;
  159.   (* The CVT is a simple storage whose values are defined and used *)
  160.   (* by the font and glyphs programs                               *)
  161.  
  162.  
  163.  
  164. const
  165.   CTTFlagTouchedX = $01;  (* X touched flag *)
  166.   CTTFlagTouchedY = $02;  (* Y touched flag *)
  167.  
  168.  
  169.  
  170.  
  171. (*****************************************************)
  172. (*                                                   *)
  173. (*              TrueType Tables Types                *)
  174. (*                                                   *)
  175. (*****************************************************)
  176.  
  177. type
  178.   (* Graphics State                            *)
  179.   (*                                           *)
  180.   (* The Graphics State (GS) is managed by the *)
  181.   (* instruction field, but does not come from *)
  182.   (* the font file. Thus, we can use 'int's    *)
  183.   (* where needed.                             *)
  184.   (*                                           *)
  185.  
  186.   PGraphicsState = ^TGraphicsState;
  187.   TGraphicsState = record
  188.                      autoFlip                : boolean;
  189.                      controlValueCutIn       : F26dot6;
  190.                      deltaBase               : int;
  191.                      deltaShift              : int;
  192.  
  193.                      dualVector,
  194.                      projVector,
  195.                      freeVector              : TUnitVector;
  196.  
  197.                      gep0,
  198.                      gep1,
  199.                      gep2                    : int;
  200.  
  201.                      instructControl         : byte;
  202.                      loop                    : Int32;
  203.  
  204.                      minimumDistance         : F26dot6;
  205.                      roundState              : int;
  206.  
  207.                      rp0,
  208.                      rp1,
  209.                      rp2                     : int;
  210.  
  211.                      scanControl             : boolean;
  212.                      singleWidthCutIn        : F26dot6;
  213.                      singleWidthValue        : F26dot6;
  214.                    end;
  215.  
  216.  
  217.  
  218.   (* TrueType Table Directory type *)
  219.  
  220.   TTableDir = Record
  221.                 version     : FixedPoint;   (* should be $10000 *)
  222.                 numTables   : word;         (* Tables number    *)
  223.  
  224.                 searchRange,           (* These parameters are only used  *)
  225.                 entrySelector,         (* for a dichotomy search in the   *)
  226.                 rangeShift     : Word; (* directory. We ignore them       *)
  227.                end;
  228.  
  229.   (* The 'TableDir' is followed by 'numTables' TableDirEntries *)
  230.  
  231.   TTableDirEntry = Record
  232.                      Tag      : array[0..3] of Char; (*        table type *)
  233.                      CheckSum : Long;                (*    table Checksum *)
  234.                      Offset   : Long;                (* Table file offset *)
  235.                      Length   : Long;                (*      Table length *)
  236.                     end;
  237.  
  238.   TTableDirEntries = array[0..100] of TTableDirEntry;
  239.   PTableDirEntries = ^TTableDirEntries;
  240.  
  241.   (* "cmap" Table *)
  242.  
  243.   TCMapDir = Record
  244.                TableVersionNumber : UShort;  (* should be 0 *)
  245.                cMapNum            : UShort;  (* number of entries *)
  246.               end;
  247.  
  248.   (* The "cmap" is followed by cMapNum TCMapDirEntries *)
  249.  
  250.   TCMapDirEntry = Record
  251.                     PlatformID,                   (* Windows = 3          *)
  252.                     PlatformEncodingID : UShort;  (* Microsoft UGL = 1    *)
  253.                     offset             : Long;    (*       offset         *)
  254.                    end;
  255.  
  256.   (* There exists 4 distinct formats : 0,2,4 et 6
  257.      Until now, we only process format 4 *)
  258.  
  259.   TCMap4 = Record
  260.              Format,                (* should be 4 *)
  261.              Length,                (* size in bytes *)
  262.              Version,               (* version number, starting at 0 *)
  263.              segCountX2,            (* segments number * 2 *)
  264.  
  265.              SearchRange,           (* these parameteres can be useful *)
  266.              EntrySelector,         (* for a dichotomy search *)
  267.              RangeShift     : UShort;
  268.             end;
  269.  
  270.   TCMap4Segment = Record
  271.                     endCount,
  272.                     startCount,
  273.                     idDelta,
  274.                     idRangeOffset : UShort;
  275.                    end;
  276.  
  277.   (* table "maxp" of 'Maximum Profiles' *)
  278.  
  279.   TMaxProfile = Record
  280.                   Version                 : Fixed;
  281.                   numGlyphs,
  282.                   maxPoints,
  283.                   maxContours,
  284.                   maxCompositePoints,
  285.                   maxCompositeContours,
  286.                   maxZones,
  287.                   maxTwilightPoints,
  288.                   maxStorage,
  289.                   maxFunctionDefs,
  290.                   maxInstructionDefs,
  291.                   maxStackElements,
  292.                   maxSizeOfInstructions,
  293.                   maxComponentElements,
  294.                   maxComponentDepth       : UShort;
  295.                 end;
  296.  
  297.  
  298.   (* table of tag "glyph" *)
  299.  
  300.   TPointRec = record
  301.                x, y, flag : integer;
  302.               end;
  303.  
  304.   TPoints = array[0..1023] of TPointRec;
  305.   PPoints = ^TPoints;
  306.  
  307.   PGlyphContour = ^TGlyphContour;
  308.   TGlyphContour = record
  309.                    Start,
  310.                    Finish : word;
  311.                   end;
  312.  
  313.   PGlyphContours = ^TGlyphContours;
  314.   TGlyphContours = array[0..1023] of TGlyphContour;
  315.  
  316.   PGlyph = ^TGlyph;
  317.   TGlyph = Record
  318.             numberOfContours,
  319.             xMin,
  320.             yMin,
  321.             xMax,
  322.             yMax,
  323.             numberOfPoints : Short;
  324.  
  325.             Contours : PGlyphContours;
  326.             Points   : PPoints;
  327.            end;
  328.  
  329.   TGlyphs = array[0..1000] of TGlyph;
  330.   PGlyphs = ^TGlyphs;
  331.  
  332.   (* table of type "head" *)
  333.  
  334.   TLongDateTime = record
  335.                     L1,
  336.                     L2 : long;
  337.                   end;
  338.  
  339.   THeader = Record
  340.              TableVersion : FixedPoint;
  341.              FontRevision : FixedPoint;
  342.  
  343.              CheckSumAdjust : Long;
  344.              MagicNumber    : Long;
  345.  
  346.              Flags      : UShort;
  347.              UnitsPerEM : UShort;
  348.  
  349.              Created  : TLongDateTime;
  350.              Modified : TLongDateTime;
  351.  
  352.              xMin : Short;
  353.              yMin : Short;
  354.              xMax : Short;
  355.              yMax : Short;
  356.  
  357.              MacStyle      : UShort;
  358.              LowestRecPPEM : UShort;
  359.  
  360.              FontDirection     : Short;
  361.              IndexToLocFormat  : Short;
  362.              GlyphDataFormat   : Short;
  363.             end;
  364.  
  365.   (* The engine provides its own memory management, it uses a simple   *)
  366.   (* growing heap, with only Alloc/Mark/Release operations. That's the *)
  367.   (* fastest heap you can have, but forces you to manage it gently. We *)
  368.   (* do !!                                                             *)
  369.  
  370.   TMarkRecord = record
  371.                   Magic : Long;    (* Magic number to identify a valid *)
  372.                   Top   : Int;     (* Mark Record                      *)
  373.                 end;
  374.  
  375. var
  376.   MaxProfile : ^TMaxProfile;         (* Maximum Profile is a global *)
  377.                                      (* ( for now )                 *)
  378.  
  379. const
  380.   Font_Pool_Allocated : boolean = False;
  381.  
  382. procedure InitBuffer ( var Buff; size : longint );
  383. (* Inits the Font Pool's memory management *)
  384.  
  385. function  Alloc  ( Size : int; var P: Pointer ): boolean;
  386. (* Allocate a block a 'Size' bytes from the heap, and returns its *)
  387. (* address in 'P'. Returns False on failure                       *)
  388.  
  389. procedure Mark( var M : TMarkRecord );
  390. (* Mark the Font Pool's current state into 'M' *)
  391.  
  392. function  Release( M : TMarkRecord ) : boolean;
  393. (* Release the Font Pool to its previous state recorded in 'M' *)
  394. (* Returns False on failure                                    *)
  395. (* WARNING: 'M' is no longer valid after the call              *)
  396. (*          this being useful for debugging purposes           *)
  397.  
  398. implementation
  399.  
  400. const
  401.   Mark_Magic = $BABE0007;
  402.  
  403. (***********************************)
  404. (*                                 *)
  405. (*   FONT POOL MANAGEMENT          *)
  406. (*                                 *)
  407. (***********************************)
  408.  
  409. var
  410.   Buffer   : PByteArray;
  411.   SizeBuff : longint;
  412.   CurBuff  : longint;
  413.  
  414. procedure InitBuffer;
  415. begin
  416.   Buffer   := PByteArray(@Buff);
  417.   SizeBuff := Size;
  418.   CurBuff  := 0;
  419.  
  420.   Font_Pool_Allocated := True;
  421. end;
  422.  
  423.  
  424. function Alloc( Size : int; var P: Pointer ): boolean;
  425. var
  426.   L : int;
  427. begin
  428.   P     := nil;
  429.   Alloc := False;
  430.   L     := CurBuff + ( Size+3 ) and -4;
  431.  
  432.   if L >= SizeBuff then Alloc:=False
  433.    else
  434.     begin
  435.      P       := @Buffer^[CurBuff];
  436.      CurBuff := L;
  437.      Alloc   := True;
  438.     end
  439. end;
  440.  
  441. (**********)
  442. (*  Mark  *)
  443. (*        ***********************************************)
  444. (*                                                      *)
  445. (*  Gets the current heap top position and put it       *)
  446. (*  into a mark record.                                 *)
  447. (*                                                      *)
  448. (********************************************************)
  449.  
  450. procedure Mark( var M : TMarkRecord );
  451. begin
  452.   M.Magic := Mark_Magic;
  453.   M.Top   := CurBuff;
  454. end;
  455.  
  456. (*************)
  457. (*  Release  *)
  458. (*           ********************************************)
  459. (*                                                      *)
  460. (*  Sets the heap top to a previously saved position    *)
  461. (*  with 'Mark'. Returns FALSE is the mark is invalid   *)
  462. (*  or has been already released.                       *)
  463. (*                                                      *)
  464. (********************************************************)
  465.  
  466. function Release( M : TMarkRecord ) : boolean;
  467. begin
  468.   if M.Magic = Mark_Magic then
  469.     begin
  470.       M.Magic := 0;
  471.       CurBuff := M.Top;
  472.       M.Top   := -1;
  473.       Release := True;
  474.     end
  475.   else
  476.     Release := False;
  477. end;
  478.  
  479.  
  480. end.
  481.